#coding=utf-8

import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.header import Header

mail_host="smtp.163.com"
mail_user="zhuyi13167119278@163.com"
mail_pass="zhuyi8888"
 
sender = 'zhuyi13167119278@163.com'
receivers = ['zhuyi13167119278@163.com']

preCerroeFile = '2018-08-27.xls'

message = MIMEMultipart()
message.attach(MIMEText('Python 邮件发送测试...', 'plain', 'utf-8'))
message['From'] = Header("菜鸟教程", 'utf-8')
message['To'] =  Header("测试", 'utf-8')
 
subject = 'Python SMTP 邮件测试'
message['Subject'] = Header(subject, 'utf-8')

att1 = MIMEText(open(preCerroeFile, 'rb').read(), 'base64', 'utf-8')
att1["Content-Type"] = 'application/octet-stream'
# 这里的filename可以任意写，写什么名字，邮件中显示什么名字
att1["Content-Disposition"] = 'attachment; filename=' + preCerroeFile
message.attach(att1)
 
try:
    smtpObj = smtplib.SMTP() 
    smtpObj.connect(mail_host, 25)    # 25 为 SMTP 端口号
    smtpObj.login(mail_user,mail_pass)  
    smtpObj.sendmail(sender, receivers, message.as_string())
    print "邮件发送成功"
except smtplib.SMTPException:
    print "Error: 无法发送邮件"